package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.StrategyQuery;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions", regionService.selectAll());
        return "destination/index";
    }

    @RequestMapping("/listHotDestByRegionId")
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model){
        //页面需要共享的数据有左右两个集合(区域>>地点>>children)
        List<Destination> list = destinationService.selectHotDestByRegionId(regionId);
        model.addAttribute("leftDests", list.subList(0, list.size() / 2));
        model.addAttribute("rightDests", list.subList(list.size() / 2, list.size()));
        return "destination/hotdestTpl";
    }
    @RequestMapping("/guide")
    public String guide(Long id, Model model){
        List<Destination> list = destinationService.toasts(id);
        //toasts.ftl需要的两个共享数据
        model.addAttribute("toasts", list);
        model.addAttribute("dest", list.remove(list.size()-1));
        //catalogs目的地下的所有分类
        model.addAttribute("catalogs", catalogService.selectContainDetailByDestId(id));
        //查询热门的攻略(默认点击数最高的三篇)
        model.addAttribute("strategyDetails", detailService.selectReplyTop3ByDestId(id));
        //查询热门的游记(默认三篇?)
        //不用,页面加载完成会异步请求自己去获取
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo")StrategyQuery qo, Model model){
        List<Destination> list = destinationService.toasts(qo.getDestId());
        //toasts.ftl需要的两个共享数据
        model.addAttribute("toasts", list);
        model.addAttribute("dest", list.remove(list.size()-1));
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo")StrategyQuery qo, Model model){
        //catalogs目的地下的所有分类
        model.addAttribute("catalogs", catalogService.selectContainDetailByDestId(qo.getDestId()));
        //当前被选中的分类
        StrategyCatalog catalog = catalogService.selectContainDetailById(qo.getCatalogId());
        model.addAttribute("catalog", catalog);
        //第一篇攻略,包含内容
        model.addAttribute("detail",
                detailService.selectContainContentById(catalog.getDetails().get(0).getId()));
        return "destination/surveyTpl";
    }
    @RequestMapping("/travels")
    public String travels(@ModelAttribute("qo")TravelQuery qo, Model model){
        qo.setPageSize(3);
        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }

}
